// 制作路由和路由配置
import Vue from 'vue'
import VueRouter from 'vue-router';
import { storage } from "../utils/globalUtil";

Vue.use(VueRouter); // 注册路由并全局使用

export const routes = [ // 生成路由配置
  { // 主页
    path: '/',
    name: 'home',
    meta: {
      isMenu: true,
      title: '主页',
    },
    component: () => import(/* webpackChunkName: "home"*/ '../views/home/home.vue'),
    alias: '/home',
    children: [
      {
        path: '/hall',
        name: 'hall',
        meta: {
          isMenu: true,
          title: '大厅',
        },
        component: () => import(/* webpackChunkName: "home"*/ '../views/hall/index.vue'),
      },
      {
        path: '/admin',
        name: 'admin',
        meta: {
          title: '管理站功能',
        },
        component: () => import('../views/admin/index.vue'),
        children: [
          {
            path: '/admin/form',
            name: 'formTest',
            meta: {
              title: '表单',
            },
            component: () => import('../views/admin/form/index.vue')
          },
          {
            path: '/admin/table',
            name: 'tableTest',
            meta: {
              title: '表格',
            },
            component: () => import('../views/admin/table/index.vue')
          }
        ]
      },
      { // function
        path: '/function',
        name: 'function',

        meta: {
          isMenu: true,
          title: '功能',
        },
        component: () => import('../views/function/index.vue'),
        children: [
          {
            path: '/file',
            name: 'file',
            meta: {
              isMenu: true,
              title: '文件',
            },
            component: () => import('../views/function/file/index.vue')
          },
          {
            path: '/fileRead',
            name: 'fileRead',
            meta: {
              isMenu: true,
              title: '文件读取并解析',
            },
            component: () => import('../views/function/file/read/index.vue')
          },
          {
            path: '/print',
            name: 'print',

            meta: {
              isMenu: true,
              title: '打印',
            },
            component: () => import('../views/function/print/index.vue')
          },
          {
            path: '/download',
            name: 'download',

            meta: {
              isMenu: true,
              title: '下载',
            },
            component: () => import('../views/function/download/index.vue')
          },
          {
            path: '/image',
            name: 'imageDeal',

            meta: {
              isMenu: true,
              title: '图片处理',
            },
            component: () => import('../views/function/image/index.vue')
          },
          {
            path: '/drag',
            name: 'drag',
            meta: {
              isMenu: true,
              title: '拖拽功能',
            },
            component: () => import('../views/function/drag/index.vue')
          },
          {
            path: '/axios',
            name: 'axios',

            meta: {
              isMenu: true,
              title: 'axios请求参数类型',
            },
            component: () => import('../views/function/axios/index.vue')
          },
          {
            path: '/intersectionObserver',
            name: 'intersectionObserver',

            meta: {
              isMenu: true,
              title: '懒加载',
            },
            component: () => import('../views/function/lazyLoad/IntersectionObserver/index.vue')
          },
          {
            path: '/codeEditor',
            name: 'codeEditor',

            meta: {
              isMenu: true,
              title: '代码编辑器',
            },
            component: () => import('../views/function/codeEditor/index.vue')
          },
          {
            path: '/highlight',
            name: 'highlight',
            meta: {
              isMenu: true,
              title: '代码高亮',
            },
            component: () => import('../views/function/highlight/index.vue')
          },
          {
            path: '/swiper',
            name: 'swiper',

            meta: {
              isMenu: true,
              title: '轮播图',
            },
            component: () => import('../views/function/swiper/Index.vue')
          },
          {
            path: '/testExpressRequest',
            name: 'testExpressRequest',

            meta: {
              isMenu: true,
              title: '测试express请求',
            },
            component: () => import('../views/function/testExpressRequest/Index.vue')
          },
          {
            path: '/video',
            name: 'videoTest',
            meta: {
              title: '视频',
            },
            component: () => import('../views/function/video/index.vue')
          },
          {
            path: '/canvas',
            name: 'canvasDemo',
            meta: {
              title: '画布',
            },
            component: () => import('../views/function/canvas/index.vue')
          }
        ]
      },
      { // vue
        path: '/vue',
        name: 'vue',

        meta: {
          isMenu: true,
          title: 'vue',
        },
        component: () => import('../views/vue/index.vue'),
        children: [
          {
            path: '/vue/mixin',
            name: 'mixin',

            meta: {
              isMenu: true,
              title: 'vue之mixin',
            },
            component: () => import('../views/vue/mixin/Index.vue')
          },
          {
            path: '/vue/props',
            name: 'props',

            meta: {
              isMenu: true,
              title: 'vue之props',
            },
            component: () => import('../views/vue/props/Index.vue')
          },
          {
            path: '/vue/attrs&listeners',
            name: 'attrs&listeners',

            meta: {
              isMenu: true,
              title: 'vue之attrs&listeners',
            },
            component: () => import('../views/vue/attrs&listeners/index.vue')
          },
          {
            path: '/vue/emit&on&off',
            name: 'emit',

            meta: {
              isMenu: true,
              title: 'vue之emit',
            },
            component: () => import('../views/vue/emit&on&off/Index.vue')
          },
          {
            path: '/vue/slot',
            name: 'slotDemo',

            meta: {
              isMenu: true,
              title: 'vue之slot',
            },
            component: () => import('../views/vue/slot/slotDemo.vue')
          },
          {
            path: '/vue/model',
            name: 'model',

            meta: {
              isMenu: true,
              title: 'vue之v-model',
            },
            component: () => import('../views/vue/model/index.vue')
          }
        ]
      },
      { // demo
        path: '/demo',
        name: 'demo',

        meta: {
          isMenu: true,
          title: 'demo',
        },
        component: () => import('../views/demo/index.vue'),
        children: []
      },
      { // utils
        path: '/utils',
        name: 'utils',
        meta: {
          isMenu: true,
          title: '工具',
        },
        component: () => import('../views/utils/index.vue'),
        children: [
          { // strToJSON
            path: '/utils/strToJSON',
            name: 'strToJSON',
            meta: {
              isMenu: true,
              title: '字符串转JSON',
            },
            component: () => import('../views/utils/strToJSON.vue'),
            children: []
          },
        ]
      },
    ]
  },
  { // 登录
    path: '/login',
    name: 'login',
    meta: {
      isMenu: false,
      title: '登录',
    },
    component: () => import(/* webpackChunkName: "login"*/ '../views/login.vue')
  }
];

const router = new VueRouter({ // 生成路由实例，并将路由配置放入
  routes
});

router.beforeEach((to, from, next) => { // 路由守卫-路由前守卫，拦截每个路由跳转
  debugger
  if (to.path !== '/login' && !checkPremissionBySessionToken(to)) {
    next('/login')
  } else {
    next()
  }
});

/**
 * @function 检查权限
 * @method checkPremissionByPathList 通过路由权限检测跳转
 * @method checkPremissionBySessionToken 通过session的token是否存在检测跳转
 * */
function checkPremissionByPathList(to) {
  let premission = storage.get('premission') || [];
  return premission.indexOf(to.path) > -1
}
function checkPremissionBySessionToken() {
  console.log(storage.get('token'))
  return storage.get('token')
}

export default router
